package com.spyz.P2_combine_file_inputformat_wordcount;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;


/**
 * KEYIN，reduce阶段输入的key：Text(单词)
 * VALUEIN，reduce阶段输入的value：IntWritable(1)
 * KEYOUT, map阶段输出的key：Text(单词)
 * VALUEOUT, map阶段输出的value：IntWritable(1)
 */
public class WordCountReduce extends Reducer<Text, IntWritable, Text, IntWritable> {
    private IntWritable outV = new IntWritable();
    @Override
    protected void reduce(Text key, Iterable<IntWritable> values, Reducer<Text, IntWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException {
        int sum=0;
        // 累加
        for (IntWritable value : values) {
            sum+=value.get();
        }

        outV.set(sum);
        // 写出
        context.write(key,outV);
    }
}
